package com.xuyiwei.xblog.modular.article.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xuyiwei.xblog.business.article.model.Type;
import com.xuyiwei.xblog.core.base.constant.enums.CommonStatusEnum;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.util.ToolUtil;
import com.xuyiwei.xblog.business.article.model.Tags;
import com.xuyiwei.xblog.business.article.service.ITagsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * 文章标签控制器
 *
 * @author fengshuonan
 * @Date 2018-10-21 09:49:57
 */
@Controller
@RequestMapping("/article/tags")
public class TagsController extends BaseController {

    private String PREFIX = "/article/tags/";

    @Autowired
    private ITagsService tagsService;

    /**
     * 跳转到文章标签首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tags.html";
    }

    /**
     * 跳转到添加文章标签
     */
    @RequestMapping("/tags_add")
    public String tagsAdd() {
        return PREFIX + "tags_add.html";
    }

    /**
     * 跳转到修改文章标签
     */
    @RequestMapping("/tags_update/{tagsId}")
    public String tagsUpdate(@PathVariable Integer tagsId, Model model) {
        Tags tags = tagsService.selectById(tagsId);
        model.addAttribute("item",tags);
        LogObjectHolder.me().set(tags);
        return PREFIX + "tags_edit.html";
    }

    /**
     * 获取文章标签列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<List<Tags>> list(String condition) {
        Result<List<Tags>> result = Result.instance();
        try{
            Wrapper<Tags> wrapper = new EntityWrapper<>();
            wrapper.eq("status", CommonStatusEnum.USE.getKey());
            List<Tags> list = tagsService.selectList(wrapper);
            result.setData(list);
        }catch (Exception e){
            result.setError("500","获取文章标签出错");
        }
        return result;
    }

    /**
     * 新增文章标签
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(Tags tags) {
        Result<Boolean> result = Result.instance();
        try {
            tags.setCreateTime(new Date());
            tags.setUpdateTime(new Date());
            tags.setStatus(CommonStatusEnum.USE.getKey());
            tagsService.insert(tags);
            result.setData(true);
        }catch (Exception e){
            result.setError("500","文章标签新增出错");
        }
        return result;
    }

    /**
     * 删除文章标签
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Long> ids) {
        Result<Boolean> result = Result.instance();
        try {
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.tagsService.deleteTagsByIds(ids);
            result.setData(true);
        }catch (Exception e){
            result.setError("500","删除文章标签出错");
        }
        return result;
    }

    /**
     * 修改文章标签
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(Tags tags) {
        Result<Boolean> result = Result.instance();
        try {
            tags.setUpdateTime(new Date());
            tagsService.updateById(tags);
            result.setData(true);
        }catch (Exception e){
            result.setError("500","修改文章标签出错");
        }
        return result;
    }

    /**
     * 文章标签详情
     */
    @RequestMapping(value = "/detail/{tagsId}")
    @ResponseBody
    public Result<Tags> detail(@PathVariable("tagsId") Integer tagsId) {
        Result<Tags> result = Result.instance();
        try{
            Tags tags = tagsService.selectById(tagsId);
            result.setData(tags);
        }catch (Exception e){
            result.setError("500","查看文件标签出错");
        }
        return result;
    }


}
